package com.hejia.bulbul.controller;

import com.hejia.alauda.entity.StatutoryDays;
import com.hejia.alauda.service.CalendarService;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * managment - com.hejia.bulbul.controller
 * Created by clt on 15/3/24.上午10:40
 */
@Controller
@RequestMapping("calendar")
public class CalendarController extends BaseController {

    @Resource
    private CalendarService calendarService;


    @RequestMapping(value = "cancel", method = RequestMethod.POST)
    public String cancel(String[] remove, String month) {
        if (remove != null){
            for (int i = 0; i < remove.length; i++){
                calendarService.removeDays(remove[i]);
            }
        }
        return "redirect:/calendar/statutorydays?month="+month;
    }

    /**
     * 月份前后翻ajax接口
     */
    @RequestMapping(value = "change", method = RequestMethod.GET)
    @ResponseBody
    public List<StatutoryDays> change(String date) {

        try {
            Date day = DateFormatUtils.ISO_DATE_FORMAT.parse(date);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

            return calendarService.listDaysByMonth(format.format(day));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 进入页
     */
    @RequestMapping(value = "statutorydays", method = RequestMethod.GET)
    public String statutoryDays(String month, Model model) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");

        List<StatutoryDays> list = new ArrayList<>();
        if (month == null){
            list  = calendarService.listDaysByMonth(format.format(new Date()));
        }else{
            try {
                list  = calendarService.listDaysByMonth(format.format(DateFormatUtils.ISO_DATE_FORMAT.parse(month)));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        if (month == null){
            month = format1.format(new Date());
        }

        model.addAttribute("list", list);
        model.addAttribute("month", month);
        return "/calendar/statutorydays";
    }

    /**
     * 维护假日
     */
    @RequestMapping(value = "addstatutorydays", method = RequestMethod.POST)
    public String addStatutoryDays(int stat, String[] date, String[] remove, String month, RedirectAttributes redirectAttributes) {
        int j = 0;

        if (remove != null){
            for (int i = 0; i < remove.length; i++){
                calendarService.removeDays(remove[i]);
            }
        }

        if (date != null) {
            for (int i = 0; i < date.length; i++) {
                StatutoryDays statutoryDays = new StatutoryDays();

                try {
                    if(calendarService.isExist(date[i])){
                        continue;
                    }
                    Date day = DateFormatUtils.ISO_DATE_FORMAT.parse(date[i]);
                    statutoryDays.setCreateDate(new Date());
                    statutoryDays.setModifyDate(new Date());
                    if (stat == 1){
                        statutoryDays.setType(StatutoryDays.Status.STATUTORY_HOLIDAYS);
                    }else{
                        statutoryDays.setType(StatutoryDays.Status.STATUTORY_HOLIDAYS_WORK);
                    }
                    statutoryDays.setStatutoryDays(day);
                    if (!calendarService.addDays(statutoryDays)) {
                        j++;
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }

            }
        }

        if (j == 0) {
            redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
            redirectAttributes.addFlashAttribute("msg", "日期添加成功");
        } else {
            redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
            redirectAttributes.addFlashAttribute("msg", "日期添加" + j + "条失败，可能有日期没有成功添加");
        }
        return "redirect:/calendar/statutorydays?month="+month;
    }

    /**
     * 法定补假日
     */
    @RequestMapping(value = "addworkdays", method = RequestMethod.POST)
    public String addWorkDays(String[] date, RedirectAttributes redirectAttributes) {
        int j = 0;
        for (int i = 0; i < date.length; i++) {
            StatutoryDays statutoryDays = new StatutoryDays();

            try {
                Date day = DateFormatUtils.ISO_DATE_FORMAT.parse(date[i]);
                statutoryDays.setCreateDate(new Date());
                statutoryDays.setModifyDate(new Date());
                statutoryDays.setType(StatutoryDays.Status.STATUTORY_HOLIDAYS_WORK);
                statutoryDays.setStatutoryDays(day);
                if (!calendarService.addDays(statutoryDays)) {
                    j++;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

        }

        if (j == 0) {
            redirectAttributes.addFlashAttribute("status", STATUS_SUCCESS);
            redirectAttributes.addFlashAttribute("msg", "日期添加成功");
        } else {
            redirectAttributes.addFlashAttribute("status", STATUS_FAILD);
            redirectAttributes.addFlashAttribute("msg", "日期添加" + j + "条失败，可能有日期没有成功添加");
        }
        return "redirect:/calendar/statutorydays";
    }
}
